package com.mdd.admin.service.googs.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.admin.service.googs.ISalesmanGoogsService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.googs.SalesmanGoogsParam;
import com.mdd.admin.vo.goods.GoodsDetailVo;
import com.mdd.admin.vo.googs.SalesmanGoogsDetailVo;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.goods.Goods;
import com.mdd.common.entity.salesman.SalesmanGoogs;
import com.mdd.common.entity.salesman.Salesman;
import com.mdd.common.entity.goods.GoodsSpecification;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.googs.SalesmanGoogsMapper;
import com.mdd.common.mapper.salesman.SalesmanMapper;
import com.mdd.common.mapper.specification.GoodsSpecificationMapper;
import com.mdd.common.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 推销员服务实现类
 */
@Service
public class SalesmanGoogsServiceImpl extends ServiceImpl<SalesmanGoogsMapper, SalesmanGoogs> implements ISalesmanGoogsService {

	@Resource
	private GoodsMapper goodsMapper;

	@Resource
	private SalesmanMapper salesmanMapper;
    @Resource
    private GoodsSpecificationMapper goodsSpecificationMapper;

	/**
	 * 推销员服务列表
	 *
	 * @param pageParam 分页参数
	 * @param params    搜索参数
	 * @return PageResult<SalesmanGoogsListVo>
	 */
	@Override
	public PageResult<SalesmanGoogsDetailVo> list(PageParam pageParam, Map<String, String> params) {

		Integer page = pageParam.getPageNo();
		Integer limit = pageParam.getPageSize();

		LambdaQueryWrapper<SalesmanGoogs> googsLambdaQueryWrapper = new LambdaQueryWrapper<>();
		String salesmanName = params.get("salesmanName");
		if (StringUtils.hasText(salesmanName)) {
			Salesman salesman = salesmanMapper.selectOne(new LambdaQueryWrapper<Salesman>()
					.eq(Salesman::getName, salesmanName)
					.eq(Salesman::getIsDelete, GlobalConstant.NOT_DELETE));
			if(Objects.nonNull(salesman)){
				googsLambdaQueryWrapper.eq(SalesmanGoogs::getSalesmanId, salesman.getId());
			} else {
				return null;
			}
		}
		// 获取所有的推销员服务
		Page<SalesmanGoogs> salesmanGoogs = this.baseMapper.selectPage(new Page<>(page, limit), googsLambdaQueryWrapper);
//		Map<Long, Long> collect3 = salesmanGoogs.getRecords().stream().collect(Collectors.toMap(SalesmanGoogs::getProductId, SalesmanGoogs::getId));

		// 获取所有的服务id===推销员id
//		Map<Long, Long> collect2 = salesmanGoogs.getRecords().stream().collect(Collectors.toMap(SalesmanGoogs::getProductId, SalesmanGoogs::getSalesmanId));


		List<Long> salesmanIds = salesmanGoogs.getRecords().stream().map(SalesmanGoogs::getSalesmanId).collect(Collectors.toList());
		// 获取推销员id-推销员名称
		Map<Integer, String> salesmanNames = salesmanMapper.selectBatchIds(salesmanIds).stream().collect(Collectors.toMap(Salesman::getId, Salesman::getName));

		// 获取所有的服务id
		List<Long> goodIds = salesmanGoogs.getRecords().stream().map(SalesmanGoogs::getProductId).collect(Collectors.toList());
		List<Goods> goods = goodsMapper.selectBatchIds(goodIds);
		Map<Long, Goods> goodsIds = goods.stream().collect(Collectors.toMap(Goods::getId, Goods -> Goods));

		List<SalesmanGoogsDetailVo> goodsDetailVos = new ArrayList<>();
		salesmanGoogs.getRecords().forEach(salesmanGoogs1 -> {
			SalesmanGoogsDetailVo goodsDetailVo = new SalesmanGoogsDetailVo();
			BeanUtils.copyProperties(salesmanGoogs1, goodsDetailVo);
			goodsDetailVo.setPrice(salesmanGoogs1.getProductPrice());
			goodsDetailVo.setSalesmanName(salesmanNames.get(Math.toIntExact(salesmanGoogs1.getSalesmanId())));
			Goods goods1 = goodsIds.get(salesmanGoogs1.getProductId());
			if(Objects.nonNull(goods1)){
				goodsDetailVo.setImage(goodsIds.get(salesmanGoogs1.getProductId()).getImage());
				goodsDetailVo.setName(goodsIds.get(salesmanGoogs1.getProductId()).getName());
			}
			goodsDetailVos.add(goodsDetailVo);
		});

		return PageResult.iPageHandle(salesmanGoogs.getTotal(), salesmanGoogs.getCurrent(), salesmanGoogs.getSize(), goodsDetailVos);
	}

	/**
	 * 推销员服务详情
	 *
	 * @param id 主键参数
	 * @return SalesmanGoogs
	 */
	@Override
	public SalesmanGoogsDetailVo detail(Integer id) {
		SalesmanGoogs model = this.baseMapper.selectOne(
				new QueryWrapper<SalesmanGoogs>()
						.eq("id", id)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在");

		SalesmanGoogsDetailVo vo = new SalesmanGoogsDetailVo();
		QueryWrapper<SalesmanGoogs> queryWrapper = Wrappers.query();
		//添加推销员名称、服务名称
		Salesman salesman = salesmanMapper.selectOne(new LambdaQueryWrapper<Salesman>().eq(Salesman::getId, model.getSalesmanId()));
		if (!Objects.isNull(salesman)) {
			vo.setSalesmanName(salesman.getName());
		}
		Goods goods = goodsMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getId, model.getProductId()));
		if (!Objects.isNull(goods)) {
			vo.setGoodsName(goods.getName());
		}
		BeanUtils.copyProperties(model, vo);
		return vo;
	}

	/**
	 * 推销员服务新增
	 *
	 * @param salesmanGoogsParam 参数
	 */
	@Override
	public void add(SalesmanGoogsParam salesmanGoogsParam) {
		LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
		goodsLambdaQueryWrapper.eq(Goods::getIsDelete, GlobalConstant.NOT_DELETE);
		// 服务表中的 id， 查询服务是否存在
		goodsLambdaQueryWrapper.eq(Goods::getId, salesmanGoogsParam.getProductId());
		Goods goods = goodsMapper.selectOne(goodsLambdaQueryWrapper);
		if(Objects.isNull(goods)) {
			throw new OperateException("服务不存在");
		}
		LambdaQueryWrapper<GoodsSpecification> goodsSLambdaQueryWrapper = new LambdaQueryWrapper<>();
		goodsSLambdaQueryWrapper.eq(GoodsSpecification::getIsDelete, GlobalConstant.NOT_DELETE);
		goodsSLambdaQueryWrapper.eq(GoodsSpecification::getGoodsSpecificationId, goods.getId());
		GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectOne(goodsSLambdaQueryWrapper);
		// 如果不是null， 则代表是规格
		if(Objects.nonNull(goodsSpecification)) {
			salesmanGoogsParam.setProductId(goodsSpecification.getGoodsId());
		}
		SalesmanGoogs model = new SalesmanGoogs();
		model.setSalesmanId(salesmanGoogsParam.getSalesmanId());
		model.setProductId(salesmanGoogsParam.getProductId());
		model.setProductPrice(salesmanGoogsParam.getProductPrice());
		model.setCreateTime(TimeUtil.timestamp());
		this.baseMapper.insert(model);
	}

	/**
	 * 推销员服务编辑
	 *
	 * @param salesmanGoogsParam 参数
	 */
	@Override
	public void edit(SalesmanGoogsParam salesmanGoogsParam) {
		SalesmanGoogs model = this.baseMapper.selectOne(
				new QueryWrapper<SalesmanGoogs>()
						.eq("id", salesmanGoogsParam.getId())
						.last("limit 1"));

		Assert.notNull(model, "数据不存在!");

		model.setId(salesmanGoogsParam.getId());
		model.setSalesmanId(salesmanGoogsParam.getSalesmanId());
		model.setProductId(salesmanGoogsParam.getProductId());
		model.setProductPrice(salesmanGoogsParam.getProductPrice());
		this.baseMapper.updateById(model);
	}

	@Override
	public void del(Integer id){
		SalesmanGoogs model = this.baseMapper.selectOne(
				new QueryWrapper<SalesmanGoogs>()
						.eq("id", id)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在!");

		this.baseMapper.deleteById(id);
	}

}
